/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.uml2.uml.Lifeline;

/**
 * Clears the "CoveredBy" attribute of a lifeline.
 * <p>
 * The following attributes must be set for the command to execute successfully:
 * </p>
 * <ul>
 * <li>Lifeline
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 * @see CoveredByCommand
 */
public class UncoveredByCommand extends CoveredByCommand {

	private Lifeline lifeline;
	private List oldFragments;

	public UncoveredByCommand() {
	}

	public UncoveredByCommand(final Lifeline lifeline) {
		setLifeline(lifeline);
	}

	public void setLifeline(final Lifeline lifeline) {
		this.lifeline = lifeline;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void execute() {
		oldFragments = new ArrayList(lifeline.getCoveredBys().size());
		oldFragments.addAll(lifeline.getCoveredBys());
		lifeline.getCoveredBys().clear();
	}

	@SuppressWarnings("unchecked")
	@Override
	public void undo() {
		lifeline.getCoveredBys().addAll(oldFragments);
		oldFragments = null;
	}

	@Override
	public void redo() {
		execute();
	}

	@Override
	public void dispose() {
		oldFragments = null;
		lifeline = null;
	}
}
